using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

namespace CodeStory.WebControls
{
	public class PropertyGridItem : IStateManager
	{
		private StateBag viewState;
		private bool isTrackingViewState;
		private PropertyGridItemCollection propertyItems;
		private PropertyGridItem _parent;
		private PropertyGrid propertyGrid;
		private int _depth;
		private bool isRoot;
		private string _internalValuePath;

		public PropertyGridItem() {
			_depth = -2;
		}

		public PropertyGridItem(PropertyGrid owner, bool isRoot) : this() {
			this.propertyGrid = owner;
			this.isRoot = isRoot;
		}

		public string Name {
			get {
				object obj = ViewState["Name"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["Name"] = value; }
		}
		public string DisplayName {
			get {
				object obj = ViewState["DisplayName"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["DisplayName"] = value; }
		}
		public string Description {
			get {
				object obj = ViewState["Description"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["Description"] = value; }
		}
		public object PropertyValue {
			get { return ViewState["PropertyValue"]; }
			set { ViewState["PropertyValue"] = value; }
		}
		public Type EditorType {
			get {
				object obj = ViewState["EditorType"];
				return obj == null ? typeof(TextBoxPropertyEditor) : (Type)obj;
			}
			set { ViewState["EditorType"] = value; }
		}
		public bool IsReadOnly {
			get {
				object obj = ViewState["IsReadOnly"];
				return obj == null ? false : (bool)obj;
			}
			set { ViewState["IsReadOnly"] = value; }
		}
		public bool StandardValuesSupported {
			get {
				object obj = ViewState["StandardValuesSupported"];
				return obj == null ? false : (bool)obj;
			}
			set { ViewState["StandardValuesSupported"] = value; }
		}
		public bool StandardValuesExclusive {
			get {
				object obj = ViewState["StandardValuesExclusive"];
				return obj == null ? false : (bool)obj;
			}
			set { ViewState["StandardValuesExclusive"] = value; }
		}
		public string StandardValueString {
			get {
				object obj = ViewState["StandardValueString"];
				return obj == null ? string.Empty : (string)obj;
			}
			set { ViewState["StandardValueString"] = value; }
		}

		public bool CustomEditor {
			get {
				object obj = ViewState["CustomEditor"];
				return obj == null ? false : (bool)obj;
			}
			set { ViewState["CustomEditor"] = value; }
		}

		protected StateBag ViewState {
			get {
				if (viewState == null) {
					viewState = new StateBag();
					if (IsTrackingViewState) {
						((IStateManager)viewState).TrackViewState();
					}
				}
				return viewState;
			}
		}

		public bool HasPropertyItems {
			get {
				if (propertyItems == null) {
					return false;
				}
				return propertyItems.Count > 0;
			}
		}

		public PropertyGridItemCollection PropertyItems {
			get {
				if (propertyItems == null) {
					propertyItems = new PropertyGridItemCollection(this);
					if (IsTrackingViewState) {
						((IStateManager)propertyItems).TrackViewState();
					}
				}
				return propertyItems;
			}
		}
		public PropertyGrid PropertyGrid {
			get { return propertyGrid; }
		}
		public PropertyGridItem Parent {
			get {
				if (this._parent != null && !this._parent.isRoot) {
					return _parent;
				}
				return null;
			}
		}
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public int Depth {
			get {
				if (this._depth == -2) {
					if (this.isRoot) {
						return -1;
					}
					if (this.Parent == null) {
						if (this.propertyGrid == null) {
							return 0;
						}
						this._depth = this.InternalValuePath.Split(new char[] {'\\'}).Length - 1;
					} else {
						this._depth = this.Parent.Depth + 1;
					}
				}
				return this._depth;
			}
		}
		internal string InternalValuePath {
			get {
				if (this._internalValuePath == null) {
					if (this._parent == null) {
						return string.Empty;
					}
					List<string> list = new List<string>();
					list.Add(PropertyGrid.Escape(this.Name));
					for (PropertyGridItem item = this._parent; (item != null) && !item.isRoot; item = item._parent) {
						if (item._internalValuePath != null) {
							list.Add(item._internalValuePath);
							break;
						}
						list.Add(PropertyGrid.Escape(item.Name));
					}
					list.Reverse();
					this._internalValuePath = string.Join('\\'.ToString(), list.ToArray());
				}
				return this._internalValuePath;
			}
		}

		internal void SetParent(PropertyGridItem parent) {
			this._parent = parent;
		}

		#region IStateManager Members
		public bool IsTrackingViewState {
			get { return this.isTrackingViewState; }
		}

		public void LoadViewState(object state) {
			object[] array = (object[])state;
			if (array != null) {
				if (array[0] != null) {
					((IStateManager)ViewState).LoadViewState(array[0]);
				}
				if (array[1] != null) {
					((IStateManager)PropertyItems).LoadViewState(array[1]);
				}
			}
		}

		public object SaveViewState() {
			object[] array = new object[2];
			if (viewState != null) {
				array[0] = ((IStateManager)viewState).SaveViewState();
			}
			if (propertyItems != null) {
				array[1] = ((IStateManager)propertyItems).SaveViewState();
			}
			if (array[0] != null || array[1] != null) {
				return array;
			} else {
				return null;
			}
		}

		public void TrackViewState() {
			this.isTrackingViewState = true;
		}
		#endregion

		internal void SetDirty() {
			ViewState.SetDirty(true);
			if (this.propertyItems != null) {
				this.propertyItems.SetDirty();
			}
		}
	}
}